var bmap = require('../../libs/bmap-wx.js');
var city = require('../../libs/city.js')
var lineHeight = 0;
var endWords = "";
var isNum;
var weatherdata="123";
var daily=[];
var lifestyle="";
Page({
  data: {
    weatherData:[],
    weatherData2:"",
    "hidden": true,
    cityName: "", 
    show:false,
   kk:""
  },
  onLoad: function () {
    var that = this;
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'u3e1NGr2G9eZRbTWhulnGmE63L62G6ZZ'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
      var weatherData = data.currentWeather[0];
      weatherData =  weatherData.currentCity;
      that.setData({
        weatherData: weatherData,
      });
     setTimeout(function(){
       console.log(weatherData);
     },3000);
    }
    // 发起weather请求 
    BMap.weather({
      fail: fail,
      success: success
    });
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
    var cityChild = city.City[0];
    var that = this;
    
    wx.getSystemInfo({
      success: function (res) {
        lineHeight = (res.windowHeight - 100) / 22;
        console.log(res.windowHeight - 100)
        that.setData({
          city: cityChild,
          winHeight: res.windowHeight - 40,
          lineHeight: lineHeight
        })
      }
    })

    setTimeout(function(){
      that.getData();
      that.getData2();
      that.getData3();
    },4000);
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
 
  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  //触发全部开始选择
  chStart: function () {
    this.setData({
      trans: ".3",
      hidden: false
    })
  },
  //触发结束选择
  chEnd: function () {
    this.setData({
      trans: "0",
      hidden: true,
      scrollTopId: this.endWords
    })
  },
  //获取文字信息
  getWords: function (e) {
    var id = e.target.id;
    this.endWords = id;
    isNum = id;
    this.setData({
      showwords: this.endWords
    })
  },
  //设置文字信息
  setWords: function (e) {
    var id = e.target.id;
    this.setData({
      scrollTopId: id
    })
  },

  // 滑动选择城市
  chMove: function (e) {
    var y = e.touches[0].clientY;
    var offsettop = e.currentTarget.offsetTop;
    var height = 0;
    var that = this;
    ;
    var cityarr = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"]
    // 获取y轴最大值
    wx.getSystemInfo({
      success: function (res) {
        height = res.windowHeight - 10;
      }
    });

    //判断选择区域,只有在选择区才会生效
    if (y > offsettop && y < height) {
      // console.log((y-offsettop)/lineHeight)
      var num = parseInt((y - offsettop) / lineHeight);
      endWords = cityarr[num];
      // 这里 把endWords 绑定到this 上，是为了手指离开事件获取值
      that.endWords = endWords;
    };


    //去除重复，为了防止每次移动都赋值 ,这里限制值有变化后才会有赋值操作，
    //DOTO 这里暂时还有问题，还是比较卡，待优化
    if (isNum != num) {
      // console.log(isNum);
      isNum = num;
      that.setData({
        showwords: that.endWords
      })
    }
  },
  //选择城市，并让选中的值显示在文本框里
  bindCity: function (e) {
    console.log(e);
    var cityName = e.currentTarget.dataset.city;
    this.setData({ cityName: cityName,weatherData:cityName })

  },
  showf:function(){
    this.setData({show:false});
    this.getData();
    this.getData2();
    this.getData3();
  },
  onPullDownRefresh:function() {
      var that=this;
    var weather3 = "";
    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.request({
      url: "https://free-api.heweather.com/s6/weather/now?location=" + that.data.weatherData + "&key=9b9b2afc48af4c2c9d60dee0d96023b6",
      data: "",
      //header:{...}用啥设置啥，我这里什么都不需要修改
      method: "GET",
      dataType: "json",//若设置json则直接返回的是对象，若其他返回貌似是String
      success: function (res) {
         weather3 = res.data.HeWeather6[0];
        var weather2 = res.data.HeWeather6[0].update.loc;
        if (weather2 != that.data.weatherData2){
          console.log("更新了" + weather2 + "---" + that.data.weatherData2);
          wx.showToast({
            title: '更新成功',
            mask: true,
            icon: 'success'
          })
          that.setData({ weatherdata: weather3, weatherData2: weather2 });
        }else{
          wx.showToast({
            title: '已是最新',
            mask: true,
            icon: 'info',

          })
          wx.hideNavigationBarLoading();
          // 停止下拉动作
          wx.stopPullDownRefresh();
          console.log("已是最新");
        }
        
      },
      fail: function (e) {

      },
      complete: function (obj) {

      }
    })

  },
  getData: function () {
    var that=this;
    var weather="";
    var weather2="";
   console.log(that.data.weatherData);
    wx.request({
      url: "https://free-api.heweather.com/s6/weather/now?location=" +that.data.weatherData+"&key=9b9b2afc48af4c2c9d60dee0d96023b6",
      data: "",
      //header:{...}用啥设置啥，我这里什么都不需要修改
      method: "GET",
      dataType: "json",//若设置json则直接返回的是对象，若其他返回貌似是String
      success: function (res) {
        weather=res.data.HeWeather6[0];
        weather2 = res.data.HeWeather6[0].update.loc;
        that.setData({ weatherdata: weather,weatherData2:weather2 });
      },
      fail: function (e) {

      },
      complete: function (obj) {

      }
    })
   
  },
  getData2: function () {
    var that = this;
    var weather = "";
    wx.request({
      url: "https://free-api.heweather.com/s6/weather/forecast?location=" + that.data.weatherData+"&key=9b9b2afc48af4c2c9d60dee0d96023b6",
      data: "",
      //header:{...}用啥设置啥，我这里什么都不需要修改
      method: "GET",
      dataType: "json",//若设置json则直接返回的是对象，若其他返回貌似是String
      success: function (res) {
        weather = res.data.HeWeather6[0];
        that.setData({ daily: weather });
      },
      fail: function (e) {

      },
      complete: function (obj) {

      }
    })

  },
  getData3: function () {
    var that = this;
    var weather = "";
    wx.request({
      url: "https://free-api.heweather.com/s6/weather/lifestyle?location=" + that.data.weatherData+"&key=9b9b2afc48af4c2c9d60dee0d96023b6",
      data: "",
      //header:{...}用啥设置啥，我这里什么都不需要修改
      method: "GET",
      dataType: "json",//若设置json则直接返回的是对象，若其他返回貌似是String
      success: function (res) {
        weather = res.data.HeWeather6[0].lifestyle;
        that.setData({ lifestyle: weather });
      },
      fail: function (e) {

      },
      complete: function (obj) {

      }
    })

  },
  ok:function(){
    this.setData({show:true});
    
  }
})